//
//  JProductDetailHeadView.m
//  HeadIndustry
//
//  Created by jun on 2017/8/28.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JProductDetailHeadView.h"
#import <UIImageView+YYWebImage.h>
#import "JHeadImgModel.h"

@implementation JProductDetailHeadView

- (instancetype)initWithFrame:(CGRect)frame imgBanners:(NSArray *)imgArr;
{
    if (self = [super initWithFrame:frame]) {
        if (!imgArr)
        {
            _imgDataArr = [NSMutableArray arrayWithArray:imgArr];
        }
        else
        {
            _imgDataArr = [NSMutableArray array];
        }
        [self setUpView];
        [self setImgView];
    }
    return self;
}

- (void)setUpView {
    _cycleView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height)];
    _cycleView.delegate = self;
    _cycleView.pagingEnabled = YES;
    _cycleView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_cycleView];
    
    _pageControl = [[UIPageControl alloc]init];
    _pageControl.left = _cycleView.width/2.0 - 25;
    _pageControl.top = _cycleView.height - 40;
    _pageControl.size = CGSizeMake(50, 15);
    [self addSubview:_pageControl];

}

- (void)setImgView {
    if (_imgDataArr.count == 0) {
        return;
    }
    _pageControl.numberOfPages = _imgDataArr.count;
    _pageControl.pageIndicatorTintColor = [UIColor grayColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    
    for (int i = 0; i < _imgDataArr.count; i++) {
        JHeadImgModel *model = _imgDataArr[i];
        UIImageView *img = [[UIImageView alloc]init];
        img.top = 0;
        img.left = self.width * i;
        img.size = self.size;
        img.userInteractionEnabled = YES;
        dispatch_async(dispatch_get_main_queue(), ^{
            [img setImageWithURL:[NSURL URLWithString:model.ImgUrl] options:YYWebImageOptionUseNSURLCache];
            [_cycleView addSubview:img];
        });
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureMethod)];
        [img addGestureRecognizer:tap];

    }
    _cycleView.contentSize = CGSizeMake(self.width * _imgDataArr.count, self.height);
}

- (void)tapGestureMethod
{
    if (_doneGsture) {
        _doneGsture();
    }
}

- (void)setBennersArr:(NSArray *)bennersArr
{
    _bennersArr = bennersArr;
    _imgDataArr = [NSMutableArray arrayWithArray:_bennersArr];
    
    [self setImgView];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSInteger index = scrollView.contentOffset.x / self.width;
    _pageControl.currentPage = index;
}

@end
